package com.gexiaobao.pigeon.ui.fragment.mine.pigeon

import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initFooter
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.ext.showLoading
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.app.weight.recycleview.SpaceItemDecoration
import com.gexiaobao.pigeon.databinding.FragmentTrainingRecordBinding
import com.gexiaobao.pigeon.ui.adapter.TrainingRecordPigeonAdapter
import com.gexiaobao.pigeon.viewmodel.MyPigeonViewModel
import com.kingja.loadsir.core.LoadService

/**
 * @Author      : hxw
 * @Date        : 2023/5/31 17:16
 * @Describe    : 训放记录
 */
class FragmentTrainingRecord : BaseFragment<MyPigeonViewModel, FragmentTrainingRecordBinding>() {

    private val mAdapter: TrainingRecordPigeonAdapter by lazy { TrainingRecordPigeonAdapter(arrayListOf()) }

    //界面状态管理者
    private lateinit var loadsir: LoadService<Any>

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.layoutList.swipeRecyclerView.init(LinearLayoutManager(context), mAdapter).let {
            it.addItemDecoration(SpaceItemDecoration(0, Util.dp2px(context, 5f)))
            it.initFooter {
                //触发加载更多时请求数据
                mViewModel.getPigeonTraining(false, mPigeonId)
            }
        }

        //状态页配置
        loadsir = loadServiceInit(mDatabind.layoutList.swipeRefreshLayout) {
            //点击重试时触发的操作
            loadsir.showLoading()
            mViewModel.getPigeonTraining(true, mPigeonId)
        }

        mDatabind.layoutList.swipeRefreshLayout.init {
            loadsir.showLoading()
            mViewModel.getPigeonTraining(true, mPigeonId)
        }
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        loadsir.showLoading()
        mViewModel.getPigeonTraining(true, mPigeonId)
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.trainingRecordResult.observe(viewLifecycleOwner) {
            if (it.isSuccess) {
                loadListData(it, mAdapter, loadsir, mDatabind.layoutList.swipeRecyclerView, mDatabind.layoutList.swipeRefreshLayout)
            } else {
                RxToast.showToast(it.errMessage)
            }
        }
    }

    companion object {

        private var mPigeonId = ""
        fun newInstance(pigeonId: String): FragmentTrainingRecord {
            mPigeonId = pigeonId
            val fragment = FragmentTrainingRecord()
            val bundle = Bundle()
            fragment.arguments = bundle
            return fragment
        }
    }
}